package cn.bytecollege.controller;

import cn.bytecollege.constant.Resource;
import cn.bytecollege.entity.Admin;
import cn.bytecollege.service.AdminService;
import cn.bytecollege.util.MD5Util;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "AdminAddController", value = "/adminAdd")
public class AdminAddController extends HttpServlet {
    private AdminService adminService = new AdminService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher(Resource.PAGE_LOCATION+"adminAdd.jsp").forward(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接收请求参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        //封装数据
        Admin admin = new Admin();
        admin.setAdminName(username);
        admin.setAdminPassword(MD5Util.md5Encode(password));

        //持久化到数据库
        adminService.save(admin);
        //添加成功跳转到首页
        response.sendRedirect(request.getContextPath()+"/index.jsp");
    }
}
